package com.boarsoft.wechat.biz.impl;

import java.util.List;

import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.dao.SimpleDao;
import com.boarsoft.wechat.biz.MsgTypeBiz;
import com.boarsoft.wechat.entity.MsgType;

@Transactional("wechatTransactionManager")
@Component("msgTypeBiz")
public class MsgTypeBizImpl implements MsgTypeBiz {

	@Autowired
	@Qualifier("wechatSimpleDao")
	protected SimpleDao dao;

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public PagedResult<MsgType> list(String templateCode, String orderBy, int pageNo, int pageSize) {
		StringBuilder hql = new StringBuilder();
		hql.append(" FROM ").append(MsgType.class.getName()).append(" AS type");
		hql.append(" INNER JOIN type.wechatTemplate AS wechatTemplate");
		if (Util.strIsNotEmpty(templateCode)) {
			hql.append(" WHERE wechatTemplate.code = ? ");
		}
		
		Query<Number> query1 = dao.createQuery("SELECT COUNT(*) ".concat(hql.toString()));
		if (Util.strIsNotEmpty(templateCode)) {
			query1.setParameter(0, templateCode);
		}
		
		int total = ((Number) query1.uniqueResult()).intValue();
		
		hql = new StringBuilder();
		hql.append(" FROM ").append(MsgType.class.getName()).append(" AS type");
		hql.append(" INNER JOIN fetch type.wechatTemplate AS wechatTemplate");
		if (Util.strIsNotEmpty(templateCode)) {
			hql.append(" WHERE wechatTemplate.code = ? ");
		}
		
		if (Util.strIsNotEmpty(orderBy)) {
			hql.append(" ORDER BY ").append("type.").append(orderBy);
		} else {
			hql.append(" ORDER BY ").append("type.").append(" id ");
		}

		Query<MsgType> query2 = dao.createQuery("SELECT type ".concat(hql.toString()));
		if (Util.strIsNotEmpty(templateCode)) {
			query2.setParameter(0, templateCode);
		}
		query2.setFirstResult((pageNo - 1) * pageSize);
		query2.setMaxResults(pageSize);

		List<MsgType> types = query2.list();
		for (MsgType msgType : types) {
			dao.evict(msgType);
		}
		return new PagedResult<MsgType>(total, types, pageNo, pageSize);
	}

	@Override
	@Transactional
	public void save(MsgType msgType) {
		// ID不为空，执行更新操作
		if (Util.strIsNotEmpty(msgType.getId())) {
			MsgType msgTypeNow = dao.get(MsgType.class, msgType.getId());
			msgTypeNow.setMsgCode(msgType.getMsgCode());
			msgTypeNow.setMemo(msgType.getMemo());
			msgTypeNow.setWechatTemplate(msgType.getWechatTemplate());
			dao.update(msgTypeNow);
		} else {
			// 增加操作
			dao.save(msgType);
		}
	}

	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(MsgType.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public MsgType get(String id) {
		StringBuilder hql = new StringBuilder();
		hql.append(" FROM ").append(MsgType.class.getName()).append(" AS type");
		hql.append(" INNER JOIN fetch type.wechatTemplate AS wechatTemplate");
		hql.append(" type.id = ?");
		Query<MsgType> query = dao.createQuery("SELECT type ".concat(hql.toString()));
		query.setParameter(0, id);
		
		List<MsgType> msgTypes = query.list();
		if (msgTypes != null && msgTypes.size() > 0) {
			return msgTypes.get(0);
		}
		return null;
	}

	public SimpleDao getDao() {
		return dao;
	}

	public void setDao(SimpleDao dao) {
		this.dao = dao;
	}
}
